<?php
class usermodel {
		var $db;
		var $base;
		function usermodel(&$base){
			$this->base = $base;
			$this->db = $base->db;
		}
		function check_username($username){
			$arr = $this->db->fetch_all("SELECT * FROM ".AM_DBTABLEPRE."users WHERE username='$username'");
				return $arr;
		}
		function get_username($uid)
		{
			$result = $this->db->fetch_first("SELECT username FROM ".AM_DBTABLEPRE."users WHERe user_id={$uid} LIMIT 0,1");
			return $result['username'];
		}
		function register($arr){
			$result = $this->db->query("INSERT INTO ".AM_DBTABLEPRE."users VALUES ('','{$arr['username']}','{$arr['password']}','{$arr['email']}','time()','time()','','1','0')");
			return $this->db->insert_id();
		}
		function login($username,$password){
			$result = $this->db->fetch_all("SELECT * FROM ".AM_DBTABLEPRE."users WHERE username='$username' and password='$password'");
			return !empty($result)?$result[0]:0;
		}
		function changepass($username,$oldpass,$newpass)
		{
			$result = $this->db->query("UPDATE ".AM_DBTABLEPRE."users SET password='{$newpass}' WHERE username='$username' and password='$oldpass'");
			return $this->db->affected_rows();
		}
		function update($row)
		{
			$uid = $_SESSION['user_id'];
			$sql = 'UPDATE '.AM_DBTABLEPRE.'users SET ';
			foreach($row as $key=>$value)
			{
				$sql.="$key".'='."$value".',';
			}
			$sql = substr($sql,0,-1);
			$result = $this->db->query("{$sql} "."WHERE user_id={$uid}" );
			return $result;			
		}
		function userdata()
		{
			$uid = $_SESSION['user_id'];
			$result = $this->db->fetch_all("SELECT *FROM ".AM_DBTABLEPRE."users WHERE user_id={$uid} LIMIT 1");
			return $result;
		}
		function get_hot($from,$num)
		{
			$result = $this->db->fetch_all("SElECT * FROM ".AM_DBTABLEPRE."users ORDER BY created DESC LIMIT {$from},{$num}");
			return $result;
		}
		function get_count()
		{
			$result = $this->db->fetch_first("SElECT COUNT(*) FROM ".AM_DBTABLEPRE."users");
			return $result['COUNT(*)'];
		}
		function indexuser($from,$num)
		{
			$result = $this->db->fetch_all("SElECT user_id,username FROM ".AM_DBTABLEPRE."users ORDER BY created DESC LIMIT {$from},{$num}");
			return $result;			
		}
	}
	?>